import { createRouter, createWebHistory } from "vue-router";
import { RouteLocationNormalized, NavigationGuardNext } from "vue-router";

// 创建路由器对象
const router = createRouter({
  // 路由模式hash或者默认不带#
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      redirect: "/home"
    },
    {
      path: "/home",
      name: "home",
      component: () => import("@/views/HelloWorld/index.vue")
    }
  ],
  strict: false,
  // 滚动行为
  scrollBehavior() {
    return {
      left: 0,
      top: 0
    };
  }
});

/**
 * @description 前置路由
 */
router.beforeEach(async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
  next();
});

/**
 * @description 路由跳转错误
 */
router.onError(error => {
  console.warn("路由错误", error.message);
});

export default router;
